#include "..\app\ASAppConst.h"
#include "ASFTTypeScreen.h"
#include "ASScreenMgr.h"
#include "ASScreenShare.h"
#include "..\control\ASList.h"
#include "..\app\ASFTData.h"
#include "..\app\ASFTIo.h"
#include "..\control\ASScrollText.h"
#include "..\control\ASListItem.h"
#include "ASFTDataScreen.h"
#include "ASFTDetailScreen.h"
#include "ASConfirmScreen.h"
#include "ASCustomScreen.h"
#include "ASPopupScreen.h"
#include "ASFTSettingScreen.h"

static as_ft_type s_as_ft_type;
static as_fttype_screen* s_as_fttype_scr_p;

static void as_fttype_screen_package_list(as_list* lp);
static void as_fttype_screen_repaint_bottom_bar(void);

static void as_fttype_screen_create_top_bar(as_screen* screen_p, as_rect area)
{
    ASINT w, h;
    as_image* img;
    as_screen_create_bar_img(screen_p, area, IMG_TOP);
    w = IMG_OP_TITLE_W;
    area.x += (area.w - w) / 2;
    area.w = w;
    h = IMG_OP_TITLE_H;
    area.y += (area.h - h) / 2;
    area.h = h;

    if(as_fttype_get_type() == AS_FT_RECORD_TYPE)
        img = as_screen_create_bar_img(screen_p, area, IMG_OP_TITLE_TYPE);
    else if(as_fttype_get_type() == AS_FT_MEMBER)
        img = as_screen_create_bar_img(screen_p, area, IMG_OP_TITLE_MEMBER);
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_TOP || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB)
        img = as_screen_create_bar_img(screen_p, area, IMG_OP_TITLE_PAY);
    else if(as_fttype_get_type() == AS_FT_TYPE_INCOME)
        img = as_screen_create_bar_img(screen_p, area, IMG_OP_TITLE_INCOME);
    else if(as_fttype_get_type() == AS_FT_TYPE_SHOP)
        img = as_screen_create_bar_img(screen_p, area, IMG_OP_TITLE_SHOP);
    else if(as_fttype_get_type() == AS_FT_TYPE_REC_EDIT_ACTION
            || as_fttype_get_type() == AS_FT_TYPE_INCOME_MGR
            || as_fttype_get_type() == AS_FT_TYPE_MEMBER_MGR
            || as_fttype_get_type() == AS_FT_TYPE_PAY_MGR
            || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
        img = as_screen_create_bar_img(screen_p, area, IMG_OP_TITLE_OPTION);
    else
        img = ASNULL;

    if(img)
    {
        img->set_show_type((as_control*)img, AS_IMAGE_NORMAL);
    }
}

static void as_fttype_rec_edit_action_lsk(ASINT idx);
static void as_fttype_setting_mgr_lsk(as_ft_type type, ASINT sidx);

static void as_fttype_screen_lsk(void)
{
    ASINT idx = s_as_fttype_scr_p->lp->select_idx;
    as_ft_type type = as_fttype_get_type();

    if(type == AS_FT_RECORD_TYPE)
    {
        as_screen_go_back();
        as_ftdata_screen_set_type(idx);
    }
    else if(type == AS_FT_MEMBER)
    {
        as_screen_go_back();
        as_ftdata_screen_set_member(idx);
    }
    else if(type == AS_FT_TYPE_PAY_TOP)
    {
        as_fttype_set_type(AS_FT_TYPE_PAY_SUB);
        as_ftdata_screen_set_moneytype_top(idx);
        s_as_fttype_scr_p->lp->clean_items((as_control*)s_as_fttype_scr_p->lp);
        as_fttype_screen_package_list(s_as_fttype_scr_p->lp);
        s_as_fttype_scr_p->lp->set_selected_idx((as_control*)s_as_fttype_scr_p->lp, as_ftdata_screen_get_moneytype_sub());
        s_as_fttype_scr_p->lp->adjust_select_item_position((as_control*)s_as_fttype_scr_p->lp);
        s_as_fttype_scr_p->lp->base.paint_hdlr((as_control*)s_as_fttype_scr_p->lp);
        as_flush();
        s_as_fttype_scr_p->lsk_p->elem.label->change_text((as_control*)s_as_fttype_scr_p->lsk_p->elem.label,
                as_ufg_tmp(0, TEXT_OK));
        as_fttype_screen_repaint_bottom_bar();
        as_flush();
    }
    else if(type == AS_FT_TYPE_PAY_SUB)
    {
        as_screen_go_back();
        as_ftdata_screen_set_moneytype_sub(idx);
    }
    else if(type == AS_FT_TYPE_INCOME)
    {
        as_screen_go_back();
        as_ftdata_screen_set_moneytype_top(idx);
    }
    else if(type == AS_FT_TYPE_SHOP)
    {
        as_screen_go_back();
        as_ft_order_shop_item(idx);
    }
    else if(type == AS_FT_TYPE_REC_EDIT_ACTION)
    {
        as_fttype_rec_edit_action_lsk(idx);
        //as_screen_go_back();
    }
    else if(type == AS_FT_TYPE_INCOME_MGR
            || type == AS_FT_TYPE_MEMBER_MGR
            || type == AS_FT_TYPE_PAY_MGR
            || type == AS_FT_TYPE_PAY_SUB_MGR)
    {
        as_screen_go_back();
        as_fttype_setting_mgr_lsk(type, idx);
    }
}

static void as_fttype_screen_rsk(void)
{
    if( as_fttype_get_type() == AS_FT_RECORD_TYPE
        || as_fttype_get_type() == AS_FT_MEMBER
        || as_fttype_get_type() == AS_FT_TYPE_PAY_TOP
        || as_fttype_get_type() == AS_FT_TYPE_INCOME
        || as_fttype_get_type() == AS_FT_TYPE_SHOP
        || as_fttype_get_type() == AS_FT_TYPE_REC_EDIT_ACTION
        || as_fttype_get_type() == AS_FT_TYPE_INCOME_MGR
        || as_fttype_get_type() == AS_FT_TYPE_MEMBER_MGR
        || as_fttype_get_type() == AS_FT_TYPE_PAY_MGR
        || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
    {
        as_screen_go_back();
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB)
    {
        as_fttype_set_type(AS_FT_TYPE_PAY_TOP);
        s_as_fttype_scr_p->lp->clean_items((as_control*)s_as_fttype_scr_p->lp);
        as_fttype_screen_package_list(s_as_fttype_scr_p->lp);
        s_as_fttype_scr_p->lp->set_selected_idx((as_control*)s_as_fttype_scr_p->lp, as_ftdata_screen_get_moneytype_top());
        s_as_fttype_scr_p->lp->adjust_select_item_position((as_control*)s_as_fttype_scr_p->lp);
        s_as_fttype_scr_p->lp->base.paint_hdlr((as_control*)s_as_fttype_scr_p->lp);
        s_as_fttype_scr_p->lsk_p->elem.label->change_text((as_control*)s_as_fttype_scr_p->lsk_p->elem.label,
                as_ufg_tmp(0, TEXT_SUB_TYPE));
        as_flush();
        as_fttype_screen_repaint_bottom_bar();
    }
}

static void as_fttype_screen_repaint_bottom_bar(void)
{
    if(s_as_fttype_scr_p->bottom_p)
        s_as_fttype_scr_p->bottom_p->base.paint_hdlr((as_control*)s_as_fttype_scr_p->bottom_p);

    as_reset_clip();

    if(s_as_fttype_scr_p->lsk_p)
        s_as_fttype_scr_p->lsk_p->base.paint_hdlr((as_control*)s_as_fttype_scr_p->lsk_p);

    as_reset_clip();

    if(s_as_fttype_scr_p->rsk_p)
        s_as_fttype_scr_p->rsk_p->base.paint_hdlr((as_control*)s_as_fttype_scr_p->rsk_p);

    as_reset_clip();
    as_set_layer_clip_ex(0, &s_as_fttype_scr_p->bottom_p->base.area);
    as_flush();
}

static void as_fttype_screen_create_bottom_bar(as_screen* screen_p, as_rect area)
{
    as_rect pos;
    ASSTR lsk_text = ASNULL;
    s_as_fttype_scr_p->bottom_p = as_screen_create_bar_img(screen_p, area, IMG_BOTTOM);
    as_memcpy(&pos, &area, sizeof(as_rect));
    pos.w = TEXT_SK_W;

    if( as_fttype_get_type() == AS_FT_RECORD_TYPE
        || as_fttype_get_type() == AS_FT_MEMBER
        || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB
        || as_fttype_get_type() == AS_FT_TYPE_INCOME
        || as_fttype_get_type() == AS_FT_TYPE_REC_EDIT_ACTION
        || as_fttype_get_type() == AS_FT_TYPE_INCOME_MGR
        || as_fttype_get_type() == AS_FT_TYPE_MEMBER_MGR
        || as_fttype_get_type() == AS_FT_TYPE_PAY_MGR
        || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
        lsk_text = TEXT_OK;
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_TOP)
        lsk_text = TEXT_SUB_TYPE;
    else if(as_fttype_get_type() == AS_FT_TYPE_SHOP)
        lsk_text = TEXT_ORDER;

    s_as_fttype_scr_p->lsk_p = as_screen_create_lsk(screen_p, pos, lsk_text, as_fttype_screen_lsk);

    if(s_as_fttype_scr_p->lsk_p)
    {
        s_as_fttype_scr_p->lsk_p->add_keycode((as_control*)s_as_fttype_scr_p->lsk_p, AS_KEY_OK);
    }

    pos.x = area.x + area.w - pos.w;
    s_as_fttype_scr_p->rsk_p = as_screen_create_rsk(screen_p, pos, TEXT_BACK, as_fttype_screen_rsk);
}

static ASCHAR* as_ftdata_get_rec_edit_action_item_str(as_ft_data_edit_type item)
{
    if(item == AS_FT_REC_ACTION_EDIT)
        return TEXT_EDIT;
    else if(item == AS_FT_REC_ACTION_DELETE)
        return TEXT_DELETE;
    else
        return ASNULL;
}

static void as_fttype_screen_package_list(as_list* lp)
{
    ASINT mbr_fh = 0, cnts, i;
    ASWCHAR* buf = ASNULL;
    as_scrolltext* st_p;
    as_listitem* li_p;
    as_rect pos;
    ASUINT param[4];

    if(as_fttype_get_type() == AS_FT_RECORD_TYPE)
    {
        cnts = AS_FT_MONEY_SECTION_MAX;
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_SHOP)
    {
        cnts = AS_FT_SHOP_ITEM_MAX;
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_REC_EDIT_ACTION)
    {
        cnts = AS_FT_REC_ACTION_EDIT_TYPE_MAX;
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_INCOME_MGR
            || as_fttype_get_type() == AS_FT_TYPE_MEMBER_MGR
            || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
    {
        cnts = AS_FT_SETTING_OPT_NEW + 1;
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_MGR)
    {
        cnts = AS_FT_SETTINF_OPT_PAY_SUB + 1;
    }
    else
    {
        if(as_fttype_get_type() == AS_FT_MEMBER)
        {
            mbr_fh = as_ft_file_open(ASFT_FILE_MEMBER, -1, AS_FILE_RO);
        }
        else if(as_fttype_get_type() == AS_FT_TYPE_PAY_TOP)
        {
            mbr_fh = as_ft_file_open(ASFT_FILE_PAYTYPE, -1, AS_FILE_RO);
        }
        else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB)
        {
            mbr_fh = as_ft_file_open(ASFT_FILE_PAYTYPE, as_ftdata_screen_get_moneytype_top(), AS_FILE_RO);
        }
        else if(as_fttype_get_type() == AS_FT_TYPE_INCOME)
        {
            mbr_fh = as_ft_file_open(ASFT_FILE_INCOMETYPE, -1, AS_FILE_RO);
        }

        if(mbr_fh <= 0)
            goto END;

        cnts = as_ft_file_get_data_cnts(mbr_fh);
    }

    if(cnts <= 0)
        goto END;

    buf = as_calloc(sizeof(ASWCHAR) * FT_MEMBER_NAME_LEN);

    if(!buf)
        goto END;

    as_memcpy(&pos, &lp->base.area, sizeof(as_rect));
    pos.h = NORMAL_LIST_ITEM_H;
    param[1] = EDIT_TEXT_COLOR;
    param[2] = DATA_SHEET_BG_COLOR;

    for(i = 0; i < cnts; i++)
    {
        if(as_fttype_get_type() == AS_FT_RECORD_TYPE)
        {
            as_wstrcpy(buf, as_ucs2_from_gb2312(as_ftdata_get_internal_type_str(AS_FT_PAY + i)));
        }
        else if(as_fttype_get_type() == AS_FT_TYPE_SHOP)
        {
            as_wstrcpy(buf, as_ucs2_from_gb2312(as_ftdata_get_shop_item_str((as_ft_shop_items)i)));
        }
        else if(as_fttype_get_type() == AS_FT_TYPE_REC_EDIT_ACTION)
        {
            as_wstrcpy(buf, as_ucs2_from_gb2312(as_ftdata_get_rec_edit_action_item_str((as_ft_data_edit_type)i)));
        }
        else if(as_fttype_get_type() == AS_FT_TYPE_INCOME_MGR
                || as_fttype_get_type() == AS_FT_TYPE_MEMBER_MGR
                || as_fttype_get_type() == AS_FT_TYPE_PAY_MGR
                || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
        {
            as_wstrcpy(buf, as_ucs2_from_gb2312(as_ft_setting_screen_get_opt_str((as_ft_setting_option)i)));
        }
        else if(as_fttype_get_type() == AS_FT_MEMBER)
            as_ft_file_get_data(mbr_fh, FT_MEMBER_MAX, buf, sizeof(ASWCHAR)*FT_MEMBER_NAME_LEN, i);
        else if(as_fttype_get_type() == AS_FT_TYPE_PAY_TOP)
            as_ft_file_get_data(mbr_fh, FT_PAYTYPE_TOP_MAX, buf, sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, i);
        else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB)
            as_ft_file_get_data(mbr_fh, FT_PAYTYPE_SUB_MAX, buf, sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, i);
        else if(as_fttype_get_type() == AS_FT_TYPE_INCOME)
            as_ft_file_get_data(mbr_fh, FT_INCOMETYPE_TOP_MAX, buf, sizeof(ASWCHAR)*FT_INCOMETYPE_NAME_LEN, i);

        li_p = lp->add_item((as_control*)lp, pos);

        if(li_p)
        {
            li_p->set_bg_color((as_control*)li_p, DATA_SHEET_BG_COLOR);
            li_p->set_bg_selected_image((as_control*)li_p, as_ufg_tmp(0, IMG_NML_LIST_HL), AS_IMAGE_FROM_RES);
            param[0] = (ASUINT)buf;
            st_p = (as_scrolltext*)li_p->add_ctrl((as_control*)li_p, AS_CTRL_SCROLLTEXT, pos, 3, (void**)param);

            if(st_p)
            {
                st_p->set_valid_show_area((as_control*)st_p, &lp->base.area);
                st_p->set_focus_bg_img((as_control*)st_p, AS_IMAGE_FROM_RES, as_ufg_tmp(0, IMG_NML_LIST_HL));
            }

            li_p->set_pen_up_cb((as_control*)li_p, (as_listitem_cb)as_fttype_screen_lsk);
        }

        pos.y += pos.h;
    }

END:

    if(buf)
        as_free(buf);

    if(mbr_fh > 0)
        as_ft_file_close(mbr_fh);

    if(as_fttype_get_type() == AS_FT_RECORD_TYPE)
    {
        lp->set_selected_idx((as_control*)lp, as_ftdata_screen_get_type());
    }

    if(as_fttype_get_type() == AS_FT_TYPE_SHOP
       || as_fttype_get_type() == AS_FT_TYPE_REC_EDIT_ACTION
       || as_fttype_get_type() == AS_FT_TYPE_INCOME_MGR
       || as_fttype_get_type() == AS_FT_TYPE_MEMBER_MGR
       || as_fttype_get_type() == AS_FT_TYPE_PAY_MGR
       || as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
    {
        lp->set_selected_idx((as_control*)lp, 0);
    }
    else if(as_fttype_get_type() == AS_FT_MEMBER)
    {
        lp->set_selected_idx((as_control*)lp, as_ftdata_screen_get_member());
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_TOP || as_fttype_get_type() == AS_FT_TYPE_INCOME)
    {
        lp->set_selected_idx((as_control*)lp, as_ftdata_screen_get_moneytype_top());
    }
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB)
    {
        lp->set_selected_idx((as_control*)lp, as_ftdata_screen_get_moneytype_sub());
    }
}

static void as_fttype_screen_create_list(as_screen* screen_p, as_rect area)
{
    as_list* lp;
    lp = (as_list*)screen_p->add_ctrl(screen_p, AS_CTRL_LIST, area, 0, ASNULL);

    if(lp)
    {
        s_as_fttype_scr_p->lp = lp;
        lp->set_bg_color((as_control*)lp, DATA_SHEET_BG_COLOR);
    }

    as_fttype_screen_package_list(lp);
}

void as_fttype_screen_init(as_screen* screen_p)
{
    as_rect area;
    s_as_fttype_scr_p = (as_fttype_screen*)screen_p;
    area.w = IMG_DATE_W * 5;
    area.y = (SCR_H >> 2);
    area.h = IMG_TOP_H;
    area.x = (SCR_W - area.w) / 2;
    as_fttype_screen_create_top_bar(screen_p, area);
    area.y += area.h;
    area.h = IMG_NUMPAD_H - IMG_BOTTOM_H;
    as_fttype_screen_create_list(screen_p, area);
    area.y += area.h;
    area.h = BOTTOM_BAR_HEIGHT;
    as_fttype_screen_create_bottom_bar(screen_p, area);
}

void as_fttype_screen_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_fttype_set_type(as_ft_type type)
{
    s_as_ft_type = type;
}

as_ft_type as_fttype_get_type(void)
{
    return s_as_ft_type;
}

static void as_fttype_enter_screen(void)
{
    as_screen_manager* scr_mgr_p = as_get_screen_stack_instance();
    as_screen* scr_p;
    //as_screen_hide_adv_bar();
    scr_mgr_p->new_and_push_screen(AS_SCREEN_FTTYPE);
    scr_p = scr_mgr_p->get_cur_screen();

    if(scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}

void as_fttype_enter_record_type_screen(void)
{
    as_fttype_set_type(AS_FT_RECORD_TYPE);
    as_fttype_enter_screen();
}

void as_fttype_enter_member_screen(void)
{
    as_fttype_set_type(AS_FT_MEMBER);
    as_fttype_enter_screen();
}

void as_fttype_enter_shop_screen(void)
{
    as_fttype_set_type(AS_FT_TYPE_SHOP);
    as_fttype_enter_screen();
}

static void as_fttype_rec_edit_lsk(void)
{
    as_time t;
    ASINT idx;
    ASFILE fh = -1;

    if(as_ftdetail_screen_get_key_info(&t, &idx) != AS_OK)
        return;

    fh = as_ft_file_open(ASFT_FILE_RECORD,
                         as_ft_file_get_param_by_date(t), AS_FILE_RW);

    if(fh <= 0)
        return;

    as_ft_file_cover_data(fh, FT_RECORD_PER_MONTH_MAX, sizeof(as_ft_main_data),
                          as_ftdata_screen_get_main_data(), idx);
    as_ft_file_close(fh);
    as_ftdetail_screen_regen_list();
    as_custom_screen_set_if_goback_reload(ASTRUE);
    as_screen_go_back();
}

static void as_fttype_rec_delete_lsk(void)
{
    as_time t;
    ASINT idx;
    ASFILE fh = -1;

    if(as_ftdetail_screen_get_key_info(&t, &idx) != AS_OK)
        return;

    fh = as_ft_file_open(ASFT_FILE_RECORD,
                         as_ft_file_get_param_by_date(t), AS_FILE_RW);

    if(fh <= 0)
        return;

    as_ft_file_delete_data(fh, FT_RECORD_PER_MONTH_MAX, idx);
    as_ft_file_close(fh);
    as_ftdetail_screen_regen_list();
    as_custom_screen_set_if_goback_reload(ASTRUE);
}

static void as_fttype_rec_delete_confirm_lsk(void)
{
    as_fttype_rec_delete_lsk();
    as_screen_go_back();
}

static void as_fttype_rec_delete_confirm(void)
{
    as_show_confirm_screen(TEXT_DELETE_RECORD_CONFIRM, TEXT_OK, TEXT_CANCEL,
                           as_fttype_rec_delete_confirm_lsk, as_screen_go_back);
}

static void as_fttype_rec_edit_action_lsk(ASINT idx)
{
    if(idx == AS_FT_REC_ACTION_EDIT)
    {
        as_screen_go_back();
        as_ft_enter_edit_data_screen();
    }
    else if(idx == AS_FT_REC_ACTION_DELETE)
    {
        as_screen_go_back();
        as_fttype_rec_delete_confirm();
    }
}

static ASWCHAR s_as_fttype_tmp_str[64];

static void as_fttype_income_edit_ok(ASBOOL if_new)
{
    ASFILE fh = -1;
    ASINT idx = as_ft_setting_screen_get_level1_idx();

    if(as_wstrlen(s_as_fttype_tmp_str) <= 0)
    {
        as_show_popup_screen(TEXT_STR_EMPTY);
        return;
    }

    fh = as_ft_file_open(ASFT_FILE_INCOMETYPE, -1, AS_FILE_RW);

    if(fh <= 0)
        return;

    if(if_new)
        as_ft_file_add_data(fh, FT_INCOMETYPE_TOP_MAX, sizeof(ASWCHAR)*FT_INCOMETYPE_NAME_LEN, s_as_fttype_tmp_str);
    else
        as_ft_file_cover_data(fh, FT_INCOMETYPE_TOP_MAX, sizeof(ASWCHAR)*FT_INCOMETYPE_NAME_LEN, s_as_fttype_tmp_str, idx);

    as_ft_file_close(fh);
    //flush content
    as_ft_setting_sub_screen_update_content();
}

static void as_fttype_member_edit_ok(ASBOOL if_new)
{
    ASFILE fh = -1;
    ASINT idx = as_ft_setting_screen_get_level1_idx();

    if(as_wstrlen(s_as_fttype_tmp_str) <= 0)
    {
        as_show_popup_screen(TEXT_STR_EMPTY);
        return;
    }

    fh = as_ft_file_open(ASFT_FILE_MEMBER, -1, AS_FILE_RW);

    if(fh <= 0)
        return;

    if(if_new)
        as_ft_file_add_data(fh, FT_MEMBER_MAX, sizeof(ASWCHAR)*FT_MEMBER_NAME_LEN, s_as_fttype_tmp_str);
    else
        as_ft_file_cover_data(fh, FT_MEMBER_MAX, sizeof(ASWCHAR)*FT_MEMBER_NAME_LEN, s_as_fttype_tmp_str, idx);

    as_ft_file_close(fh);
    //flush content
    as_ft_setting_sub_screen_update_content();
}

static void as_fttype_pay_edit_ok(ASBOOL if_new)
{
    ASFILE fh = -1;
    ASINT idx = as_ft_setting_screen_get_level1_idx();
    ASINT idx2 = as_ft_setting_screen_get_level2_idx();

    if(as_wstrlen(s_as_fttype_tmp_str) <= 0)
    {
        as_show_popup_screen(TEXT_STR_EMPTY);
        return;
    }

    if(as_fttype_get_type() == AS_FT_TYPE_PAY_MGR)
        fh = as_ft_file_open(ASFT_FILE_PAYTYPE, -1, AS_FILE_RW);
    else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
        fh = as_ft_file_open(ASFT_FILE_PAYTYPE, idx, AS_FILE_RW);

    if(fh <= 0)
        return;

    if(if_new)
    {
        if(as_fttype_get_type() == AS_FT_TYPE_PAY_MGR)
            as_ft_file_add_data(fh, FT_PAYTYPE_TOP_MAX, sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, s_as_fttype_tmp_str);
        else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
            as_ft_file_add_data(fh, FT_PAYTYPE_SUB_MAX, sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, s_as_fttype_tmp_str);
    }
    else
    {
        if(as_fttype_get_type() == AS_FT_TYPE_PAY_MGR)
            as_ft_file_cover_data(fh, FT_PAYTYPE_TOP_MAX, sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, s_as_fttype_tmp_str, idx);
        else if(as_fttype_get_type() == AS_FT_TYPE_PAY_SUB_MGR)
            as_ft_file_cover_data(fh, FT_PAYTYPE_SUB_MAX, sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, s_as_fttype_tmp_str, idx2);
    }

    as_ft_file_close(fh);
    //flush content
    as_ft_setting_sub_screen_update_content();
}

static void as_fttype_income_edit_done(void)
{
    as_fttype_income_edit_ok(ASFALSE);
}

static void as_fttype_income_new_done(void)
{
    as_fttype_income_edit_ok(ASTRUE);
}

static void as_fttype_member_edit_done(void)
{
    as_fttype_member_edit_ok(ASFALSE);
}

static void as_fttype_member_new_done(void)
{
    as_fttype_member_edit_ok(ASTRUE);
}

static void as_fttype_pay_edit_done(void)
{
    as_fttype_pay_edit_ok(ASFALSE);
}

static void as_fttype_pay_new_done(void)
{
    as_fttype_pay_edit_ok(ASTRUE);
}

static void as_fttype_setting_mgr_delete(void)
{
    ASFILE fh = -1;
    ASINT idx = as_ft_setting_screen_get_level1_idx();
    ASINT idx2 = as_ft_setting_screen_get_level2_idx();
    as_ft_type type = as_fttype_get_type();
    as_screen_go_back();

    if(type == AS_FT_TYPE_INCOME_MGR)
        fh = as_ft_file_open(ASFT_FILE_INCOMETYPE, -1, AS_FILE_RW);
    else if(type == AS_FT_TYPE_MEMBER_MGR)
        fh = as_ft_file_open(ASFT_FILE_MEMBER, -1, AS_FILE_RW);
    else if(type == AS_FT_TYPE_PAY_MGR)
        fh = as_ft_file_open(ASFT_FILE_PAYTYPE, -1, AS_FILE_RW);
    else if(type == AS_FT_TYPE_PAY_SUB_MGR)
        fh = as_ft_file_open(ASFT_FILE_PAYTYPE, idx, AS_FILE_RW);

    if(fh <= 0)
        return;

    if(type == AS_FT_TYPE_INCOME_MGR)
        as_ft_file_delete_data(fh, FT_INCOMETYPE_TOP_MAX, idx);
    else if(type == AS_FT_TYPE_MEMBER_MGR)
        as_ft_file_delete_data(fh, FT_MEMBER_MAX, idx);
    else if(type == AS_FT_TYPE_PAY_MGR)
    {
        as_ft_file_delete_data(fh, FT_PAYTYPE_TOP_MAX, idx);
        as_file_delete(as_ft_get_file_path(ASFT_FILE_PAYTYPE, idx));
    }
    else if(type == AS_FT_TYPE_PAY_SUB_MGR)
    {
        as_ft_file_delete_data(fh, FT_PAYTYPE_SUB_MAX, idx2);
    }

    as_ft_file_close(fh);
    //flush content
    as_ft_setting_sub_screen_update_content();
}

static void as_fttype_income_mgr_show_delete_screen(void)
{
    as_ft_type type = as_fttype_get_type();
    as_void_func lsk = ASNULL;
    ASSTR content = ASNULL;

    if(type == AS_FT_TYPE_INCOME_MGR
       || type == AS_FT_TYPE_MEMBER_MGR
       || type == AS_FT_TYPE_PAY_SUB_MGR)
    {
        content = TEXT_DELETE_TYPE_CONFIRM;
    }
    else if(type == AS_FT_TYPE_PAY_MGR)
    {
        content = TEXT_DELETE_PAY_TOP_TYPE_CONFIRM;
    }

    lsk = as_fttype_setting_mgr_delete;
    as_show_confirm_screen(content, TEXT_OK, TEXT_CANCEL,
                           lsk, as_screen_go_back);
}

static void as_fttype_setting_mgr_lsk(as_ft_type type, ASINT sidx)
{
    if(sidx == AS_FT_SETTING_OPT_EDIT)
    {
        ASWSTR default_str = as_ft_get_tmp_wstr();
        ASINT idx = as_ft_setting_screen_get_level1_idx();
        ASINT idx2 = as_ft_setting_screen_get_level2_idx();
        ASFILE fh = -1;

        if(type == AS_FT_TYPE_INCOME_MGR)
            fh = as_ft_file_open(ASFT_FILE_INCOMETYPE, -1, AS_FILE_RO);
        else if(type == AS_FT_TYPE_MEMBER_MGR)
            fh = as_ft_file_open(ASFT_FILE_MEMBER, -1, AS_FILE_RO);
        else if(type == AS_FT_TYPE_PAY_MGR)
            fh = as_ft_file_open(ASFT_FILE_PAYTYPE, -1, AS_FILE_RO);
        else if(type == AS_FT_TYPE_PAY_SUB_MGR)
            fh = as_ft_file_open(ASFT_FILE_PAYTYPE, idx, AS_FILE_RO);

        if(fh <= 0)
            return;

        if(type == AS_FT_TYPE_INCOME_MGR)
            as_ft_file_get_data(fh, FT_INCOMETYPE_TOP_MAX, default_str,
                                sizeof(ASWCHAR)*FT_INCOMETYPE_NAME_LEN, idx);
        else if(type == AS_FT_TYPE_MEMBER_MGR)
            as_ft_file_get_data(fh, FT_MEMBER_MAX, default_str,
                                sizeof(ASWCHAR)*FT_MEMBER_NAME_LEN, idx);
        else if(type == AS_FT_TYPE_PAY_MGR)
            as_ft_file_get_data(fh, FT_PAYTYPE_TOP_MAX, default_str,
                                sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, idx);
        else if(type == AS_FT_TYPE_PAY_SUB_MGR)
            as_ft_file_get_data(fh, FT_PAYTYPE_SUB_MAX, default_str,
                                sizeof(ASWCHAR)*FT_PAYTYPE_NAME_LEN, idx2);

        as_ft_file_close(fh);
        fh = -1;
        //as_screen_hide_adv_bar();
        as_memset(s_as_fttype_tmp_str, 0, sizeof(s_as_fttype_tmp_str));
        as_wstrcpy(s_as_fttype_tmp_str, default_str);

        if(type == AS_FT_TYPE_INCOME_MGR)
        {
            as_create_input_screen(as_ufg_tmp(0, TEXT_INCOME_TYPE_MGR), default_str,
                                   AS_INPUT_NORMAL, FT_INCOMETYPE_NAME_LEN, s_as_fttype_tmp_str , as_fttype_income_edit_done);
        }
        else if(type == AS_FT_TYPE_MEMBER_MGR)
        {
            as_create_input_screen(as_ufg_tmp(0, TEXT_MEMBER_TYPE_MGR), default_str,
                                   AS_INPUT_NORMAL, FT_MEMBER_NAME_LEN, s_as_fttype_tmp_str , as_fttype_member_edit_done);
        }
        else if(type == AS_FT_TYPE_PAY_MGR
                || type == AS_FT_TYPE_PAY_SUB_MGR)
        {
            as_create_input_screen(as_ufg_tmp(0, TEXT_PAY_TYPE_MGR), default_str,
                                   AS_INPUT_NORMAL, FT_PAYTYPE_NAME_LEN, s_as_fttype_tmp_str , as_fttype_pay_edit_done);
        }
    }
    else if(sidx == AS_FT_SETTING_OPT_DELETE)
    {
        as_fttype_income_mgr_show_delete_screen();
    }
    else if(sidx == AS_FT_SETTING_OPT_NEW)
    {
        ASWSTR default_str = as_ft_get_tmp_wstr();
        ASFILE fh = -1;
        ASINT idx = as_ft_setting_screen_get_level1_idx();
        ASINT cnts;
        ASINT max_cnts;

        if(type == AS_FT_TYPE_INCOME_MGR)
        {
            fh = as_ft_file_open(ASFT_FILE_INCOMETYPE, -1, AS_FILE_RO);
            max_cnts = FT_INCOMETYPE_TOP_MAX;
        }
        else if(type == AS_FT_TYPE_MEMBER_MGR)
        {
            fh = as_ft_file_open(ASFT_FILE_MEMBER, -1, AS_FILE_RO);
            max_cnts = FT_MEMBER_MAX;
        }
        else if(type == AS_FT_TYPE_PAY_MGR)
        {
            fh = as_ft_file_open(ASFT_FILE_PAYTYPE, -1, AS_FILE_RO);
            max_cnts = FT_PAYTYPE_TOP_MAX;
        }
        else if(type == AS_FT_TYPE_PAY_SUB_MGR)
        {
            fh = as_ft_file_open(ASFT_FILE_PAYTYPE, idx, AS_FILE_RO);
            max_cnts = FT_PAYTYPE_SUB_MAX;
        }

        if(fh <= 0)
            return;

        cnts = as_ft_file_get_data_cnts(fh);

        if(cnts >= max_cnts)
        {
            as_show_popup_screen(TEXT_TYPE_FULL);
            return;
        }

        //as_screen_hide_adv_bar();
        as_memset(s_as_fttype_tmp_str, 0, sizeof(s_as_fttype_tmp_str));

        if(type == AS_FT_TYPE_INCOME_MGR)
            as_create_input_screen(as_ufg_tmp(0, TEXT_INCOME_TYPE_MGR), default_str,
                                   AS_INPUT_NORMAL, FT_INCOMETYPE_NAME_LEN, s_as_fttype_tmp_str , as_fttype_income_new_done);
        else if(type == AS_FT_TYPE_MEMBER_MGR)
            as_create_input_screen(as_ufg_tmp(0, TEXT_MEMBER_TYPE_MGR), default_str,
                                   AS_INPUT_NORMAL, FT_INCOMETYPE_NAME_LEN, s_as_fttype_tmp_str , as_fttype_member_new_done);
        else if(type == AS_FT_TYPE_PAY_MGR
                || type == AS_FT_TYPE_PAY_SUB_MGR)
            as_create_input_screen(as_ufg_tmp(0, TEXT_PAY_TYPE_MGR), default_str,
                                   AS_INPUT_NORMAL, FT_INCOMETYPE_NAME_LEN, s_as_fttype_tmp_str , as_fttype_pay_new_done);
    }
    else if(sidx == AS_FT_SETTINF_OPT_PAY_SUB)
    {
        as_ft_setting_screen_switchto_pay_sub_screen();
    }
}

void as_fttype_enter_rec_edit_action_screen(void)
{
    as_time t;
    ASINT idx;

    if(as_ftdetail_screen_get_key_info(&t, &idx) != AS_OK)
    {
        as_show_popup_screen(TEXT_EMPTY_MONTH);
        return;
    }

    as_fttype_set_type(AS_FT_TYPE_REC_EDIT_ACTION);
    as_ft_screen_prepare_enter_edit_screen(t, idx, as_fttype_rec_edit_lsk);
    as_fttype_enter_screen();
}

void as_fttype_enter_income_mgr_opt_screen(void)
{
    as_fttype_set_type(AS_FT_TYPE_INCOME_MGR);
    as_fttype_enter_screen();
}

void as_fttype_enter_member_mgr_opt_screen(void)
{
    as_fttype_set_type(AS_FT_TYPE_MEMBER_MGR);
    as_fttype_enter_screen();
}

void as_fttype_enter_pay_mgr_opt_screen(void)
{
    as_fttype_set_type(AS_FT_TYPE_PAY_MGR);
    as_fttype_enter_screen();
}

void as_fttype_enter_pay_sub_mgr_opt_screen(void)
{
    as_fttype_set_type(AS_FT_TYPE_PAY_SUB_MGR);
    as_fttype_enter_screen();
}

void as_fttype_enter_pay_or_income_screen(void)
{
    if(as_ftdata_screen_get_type() == AS_FT_PAY)
        as_fttype_set_type(AS_FT_TYPE_PAY_TOP);
    else if(as_ftdata_screen_get_type() == AS_FT_INCOME)
        as_fttype_set_type(AS_FT_TYPE_INCOME);

    as_fttype_enter_screen();
}

void as_fttype_enter_pay_sub_screen(void)
{
    as_fttype_set_type(AS_FT_TYPE_PAY_SUB);
    as_fttype_enter_screen();
}